/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.struts.actions;

import com.oea.hibernate.entities.Faculty;
import com.oea.struts.forms.FacultyRegisterForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Sony
 */
public class FacultyRegisterAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static  String path;

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FacultyRegisterForm frf=(FacultyRegisterForm)form;
        
        Faculty f=new Faculty();
        
        f.setFname(frf.getFname());
        f.setLname(frf.getLname());
        f.setMname(frf.getMname());
        f.setGender(frf.getGender());
        f.setEmail(frf.getEmail());
        f.setPassword(frf.getPassword());
        f.setContact(frf.getContact());
        f.setAddedby(frf.getAddedby());
        if(frf.getAddedby()!=null){
        f.setStatus(true);
                }
        else
        {
            f.setStatus(false);
        }
        
        Object o=request.getSession().getAttribute("usertype");
        
        if(f.add()!=null)
        {
             
           List<Faculty>  allfaculties = (List<Faculty>) new Faculty().getAll();
           List<Faculty> li_fac=new ArrayList<>();
           request.getSession().setAttribute("li_allfaculties", allfaculties);
            request.getSession().setAttribute("page_li_allfacilties", li_fac);
            int count=0;
            for (Faculty faculty : allfaculties) 
            {
                count++;
                faculty.setAdmin();
                if(count<=10)
                {
                    li_fac.add(faculty);
                }
            }
            
           
           double pagelink=Math.ceil((float)allfaculties.size()/10);
           request.getSession().setAttribute("pagelink", pagelink);
            if(o.equals("admin"))
            {
                path="view_faculty";
            }
            else
            {
            path="index";
            }
        }
        else
        {
            path="faculty_register";
        }
        
        return mapping.findForward(path);
    }
}
